<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        /**
         *  Promise是专门用来解决异步回调地狱,因此，执行器函数中99.99% 都应该是异步操作
         */

        let p = new Promise((resolve, reject)=>{
            const xhr = new XMLHttpRequest();
            xhr.open('get', 'https://api.uixsj.cn/hitokoto/get?type=social');
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    if(/^2\d{2}$/.test(xhr.status)){// 成功
                        // console.log(xhr.response);
                        resolve(xhr.response);
                    }else{// 失败
                        // console.log(xhr.status);
                        reject(xhr.status);
                    }
                }
            }
        })

        p.then(value=>{
            console.log('data:', value);
        },reason=>{
            console.log('reason: ', reason);
        })
    </script>
</body>
</html>